package com.skcc.cop.mrtaxi.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

public final class WidgetUtil {
	
	static ProgressDialog mProgress;
	static Toast mToast;

	public static void makeToastLong(Context context, final String msg) {
		final Activity act = (Activity) context;
		act.runOnUiThread(new Runnable() {
			public void run() {
				if (mToast == null) {
					mToast = Toast.makeText(act, msg, Toast.LENGTH_LONG);
				} else {
					mToast.setText(msg);
				}
				mToast.show();
			}
		});
	}

	public static void makeToastShort(Context context, final String msg) {
		final Activity act = (Activity) context;
		act.runOnUiThread(new Runnable() {
			public void run() {
				if (mToast == null) {
					mToast = Toast.makeText(act, msg, Toast.LENGTH_SHORT);
				} else {
					mToast.setText(msg);
				}
				mToast.show();
			}
		});
	}

	public static void makeToastLongLeft(Context context, final String msg) {
		final Activity act = (Activity) context;
		act.runOnUiThread(new Runnable() {
			public void run() {
				Toast.makeText(act, msg, Toast.LENGTH_LONG).show();
			}
		});
	}

	public static void makeToastShortLeft(Context context, final String msg) {
		final Activity act = (Activity) context;
		act.runOnUiThread(new Runnable() {
			public void run() {
				Toast.makeText(act, msg, Toast.LENGTH_SHORT).show();
			}
		});
	}

	public static void makeToast(Context context, final String msg, int duration) {
		Toast.makeText(context, msg, duration).show();
	}

	public static void showPopupYes(Context context, String title, String message, String btnText) {
		new AlertDialog.Builder(context).setTitle(title).setMessage(message).setPositiveButton(btnText, null).show();
	}

	public void showPopupYesNo(Context context, String title, String message, String pos, String neg) {
		new AlertDialog.Builder(context).setTitle(title).setMessage(message)
				.setPositiveButton(pos, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {

					}
				}).setNegativeButton(neg, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {

					}
				}).show();
	}

	public void showPopupYesNoCan(Context context, String title, String message, String pos, String neu, String neg) {
		new AlertDialog.Builder(context).setTitle(title).setMessage(message)
				.setPositiveButton(pos, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {

					}
				}).setNeutralButton(neu, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {

					}
				}).setNegativeButton(neg, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {

					}
				}).show();
	}

	public void showPopupListCan(Context context, final String[] items, String title, String noTitle) {

		new AlertDialog.Builder(context).setTitle(title).setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				// mDiaHandler.sendEmptyMessageDelayed(id, 10);
			}
		}).setNegativeButton(noTitle, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		}).show();
	}

	public final Handler showPopupHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
			}
		}
	};

	public static void startProgressBar(final Activity act) {
		act.runOnUiThread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				// act.showDialog(Constants.PROGRESS_ALERT);
				try {
					if (mProgress != null) {
						mProgress.dismiss();
					}

					mProgress = new ProgressDialog(act);
					mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					mProgress.setMessage("?�이�?? 로딩중입?�다...");
					mProgress.setCancelable(true);
					// false�??�면, backkey?�에 ?�무 것도 ?�한??
					mProgress.show();
				} catch (Exception e) {
				}
			}
		});
	}

	public static void startProgressBar(final Activity act, final String text) {
		act.runOnUiThread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				// act.showDialog(Constants.PROGRESS_ALERT);
				try {
					if (mProgress != null) {
						mProgress.dismiss();
					}

					mProgress = new ProgressDialog(act);
					mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
					mProgress.setMessage(text);
					mProgress.setCancelable(true);
					// false�??�면, backkey?�에 ?�무 것도 ?�한??
					mProgress.show();
				} catch (Exception e) {
				}
			}
		});
	}

	public static void endProgressBar(final Activity act) {
		act.runOnUiThread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				// act.removeDialog(Constants.PROGRESS_ALERT);
				try {
					if (mProgress != null) {
						mProgress.dismiss();
					}
				} catch (Exception e) {
				}
			}
		});
	}

	public static void showSoftKeyboard(Context context, EditText editText) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInput(editText, 0);
	}

	public static void hideSoftKeyboard(Context context, EditText editText) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
	}

	public static void exitApp(Activity ac) {
		ac.moveTaskToBack(true);
		ac.finish();
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	public static Drawable getDrawable(Context context, int resId) {
		Resources res = context.getResources();
		return res.getDrawable(resId);
	}

	public static void showTopView(Context context, View view) {
		WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE,
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_DIM_BEHIND,
				PixelFormat.TRANSLUCENT);
		mParams.gravity = Gravity.CENTER;
		mWindowManager.addView(view, mParams);
	}

	public static void hideTopView(Context context, View view) {
		((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).removeView(view);
	}
}
